#! /usr/bin/python3

# 实现 cp 命令
# $ cp /etc/passwd  /root/passwd
import sys

def copyfile(srcfilename, dstfilename):
    with open(srcfilename, 'rb') as fr:
        with open(dstfilename, 'wb') as fw:
              while True:
                  b = fr.read(1*1024*1024)
                  if not b:
                      break
                  fw.write(b)

def main():
    # 判断参数是否够用
    if len(sys.argv) < 3:
        print('''USAGE：
    mycp 源文件路径 目标文件路径''')
        return
    # 1. 用shutil.copyfile()
    # import shutil
    # shutil.copyfile(sys.argv[1], sys.argv[2])
    # 2. 用自定义的函数实现
    try:
        copyfile(sys.argv[1], sys.argv[2])
    except:
        print("复制文件失败")

main()


